package com.feeling.xiu.ui

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import com.feeling.xiu.R
import org.jetbrains.anko.startActivity


class RegisterActivity : AppCompatActivity(), RegisterFirstFragment.OnFragmentInteractionListener, RegisterSecondFragment.OnFragmentInteractionListener, RegisterThirdFragment.OnFragmentInteractionListener {

    private var currentFragment: Fragment? = null

    override fun onFragmentInteraction(eventType: String) {
        when (eventType) {
//            RegisterFirstFragment::class.java.simpleName->{ toast(eventType) }
            RegisterSecondFragment::class.java.simpleName -> {//进入注册第二步
                switchContent(RegisterSecondFragment())
            }
            RegisterThirdFragment::class.java.simpleName -> {//进入注册第 三步
                switchContent(RegisterThirdFragment())
            }

            MainActivity::class.java.simpleName -> {//进入注册第 三步
                startActivity<MainActivity>()
                finish()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        initView()
    }

    private fun initView() {

        switchContent(RegisterFirstFragment())//进入注册第一步
//        switchContent(RegisterThirdFragment())

    }

    fun switchContent(to: Fragment) {
        val fm = supportFragmentManager
        val transaction = fm.beginTransaction()
        if (currentFragment != null) {
            if (currentFragment !== to) {
//            FragmentTransaction transaction = fm.beginTransaction().setCustomAnimations(
//                    android.R.anim.fade_in, R.anim.slide_out_to_bottom);
                if (!to.isAdded()) { // 先判断是否被add过
                    transaction.hide(currentFragment).add(R.id.frameLayout_container, to)                    // 隐藏当前的fragment，add下一个到Activity中
//                            .addToBackStack(null)
                            .commit()
                } else {
                    transaction.hide(currentFragment).show(to)
//                            .addToBackStack(null)
                            .commit() // 隐藏当前的fragment，显示下一个
                }
            }
        } else {
            transaction.replace(R.id.frameLayout_container, to)
//                    .addToBackStack(null)
            transaction.commit()
        }
        currentFragment = to
    }
}
